import 'dart:async';

import 'package:cached_network_image/cached_network_image.dart';
import 'package:card_swiper/card_swiper.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart';
import 'package:get/get.dart';
import 'package:yi_shop/event/index.dart';
import 'package:yi_shop/model/brand.dart';
import 'package:yi_shop/model/popularity.dart';
import 'package:yi_shop/model/product.dart';
import 'package:yi_shop/model/superior.dart';
import 'package:yi_shop/route/index.dart';
import 'package:yi_shop/utils/colors.dart';
import 'package:yi_shop/utils/styles.dart';
import 'package:yi_shop/utils/svg.dart';

class HomeIndexController extends GetxController {
  List<_NavTabModel> navTabs = [
    _NavTabModel(name: '0元砍价', url: RoutePath.bargain),
    _NavTabModel(name: '团购专区', url: RoutePath.groupBuy),
    _NavTabModel(name: '领券中心', url: RoutePath.couponCenter),
    _NavTabModel(name: '品牌专区', url: RoutePath.brandZone),
    _NavTabModel(name: '优惠精选', url: RoutePath.favorableChoice),
    _NavTabModel(name: '幸运抽奖', url: RoutePath.luckyDraw),
    _NavTabModel(name: '话费充值', url: RoutePath.telephoneCharge),
    _NavTabModel(name: '发票助手', url: RoutePath.invoiceAssistant)
  ];

  List<String> topList = [
    'https://img0.baidu.com/it/u=4117358168,2549351276&fm=253&fmt=auto&app=138&f=JPEG?w=640&h=480',
    'https://img1.baidu.com/it/u=1936620111,432669461&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=300',
    'https://img1.baidu.com/it/u=1247406224,3137556227&fm=253&fmt=auto&app=138&f=JPEG?w=658&h=246',
    'https://img1.baidu.com/it/u=967649214,3977500688&fm=253&fmt=auto&app=138&f=JPG?w=800&h=250',
    'https://img2.baidu.com/it/u=967150179,895916412&fm=253&fmt=auto&app=138&f=JPEG?w=853&h=500'
  ];

  List<String> sales = [
    'https://img0.baidu.com/it/u=109848180,1796602448&fm=253&fmt=auto&app=138&f=JPEG?w=708&h=500',
    'https://img0.baidu.com/it/u=1897123730,610026272&fm=253&fmt=auto&app=138&f=JPEG?w=800&h=500',
    'https://img1.baidu.com/it/u=928859565,328511594&fm=253&fmt=auto&app=138&f=JPEG?w=650&h=478',
    'https://img1.baidu.com/it/u=2002870663,3527005408&fm=253&fmt=auto&app=138&f=JPEG?w=962&h=500',
    'https://img0.baidu.com/it/u=3633609166,687761423&fm=253&fmt=auto&app=138&f=JPEG?w=499&h=312',
    'https://img2.baidu.com/it/u=1413572237,4086220968&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=848',
    'https://img2.baidu.com/it/u=3159563744,3929235884&fm=253&fmt=auto&app=138&f=JPEG?w=731&h=500',
    'https://img0.baidu.com/it/u=1210922561,3202918365&fm=253&fmt=auto&app=138&f=JPEG?w=888&h=500'
  ];

  List<BrandManufactureModel> brandList = [
    BrandManufactureModel(
        name: 'MJ制造商',
        price: 9.9,
        imgUrl:
            'https://img1.baidu.com/it/u=713192780,3288894073&fm=253&fmt=auto&app=138&f=JPEG?w=549&h=500'),
    BrandManufactureModel(
        name: 'Surface制造商',
        price: 2999,
        imgUrl:
            'https://img0.baidu.com/it/u=3162832527,2865553814&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500'),
    BrandManufactureModel(
        name: 'HUAWEI制造商',
        price: 4999,
        imgUrl:
            'https://img0.baidu.com/it/u=1996343225,3808403639&fm=253&fmt=auto&app=138&f=JPG?w=400&h=400'),
    BrandManufactureModel(
        name: 'NIKON制造商',
        price: 1999,
        imgUrl:
            'https://img0.baidu.com/it/u=41002518,307079760&fm=253&fmt=auto&app=138&f=JPEG?w=250&h=242'),
  ];

  List<SuperiorModel> superiors = [
    SuperiorModel(
        name: 'Microsoft',
        logoUrl:
            'https://img0.baidu.com/it/u=2300269363,2256599470&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=469'),
    SuperiorModel(
        name: 'Apple',
        logoUrl:
            'https://img1.baidu.com/it/u=2223560061,563810333&fm=253&fmt=auto&app=138&f=JPEG?w=300&h=300'),
    SuperiorModel(
        name: 'Lenovo',
        logoUrl:
            'https://img1.baidu.com/it/u=502811127,1933021562&fm=253&fmt=auto&app=138&f=JPG?w=361&h=361'),
    SuperiorModel(
        name: 'HUAWEI',
        logoUrl:
            'https://img1.baidu.com/it/u=2588472824,1584755805&fm=253&fmt=auto&app=138&f=JPEG?w=889&h=500'),
    SuperiorModel(
        name: 'MI',
        logoUrl:
            'https://img2.baidu.com/it/u=3003666794,1772258401&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500'),
    SuperiorModel(
        name: 'OPPO',
        logoUrl:
            'https://img1.baidu.com/it/u=692379860,397628659&fm=253&fmt=auto&app=120&f=JPEG?w=569&h=325'),
  ];

  List<NewProductModel> productList = [
    NewProductModel(
        imgUrl:
            'https://gw.alicdn.com/imgextra/i3/369979583/O1CN01qux1e32Kf4Pov0iCR_!!369979583.jpg_Q75.jpg_.webp',
        name: '遥遥领先',
        price: 2999),
    NewProductModel(
        imgUrl:
            'https://img.alicdn.com/img/i1/96812084/O1CN01h21jMo1RGWI2PAmyg_!!0-saturn_solar.jpg_360x360xzq75.jpg_.webp',
        name: 'ipHone 13',
        price: 2899),
    NewProductModel(
        imgUrl:
            'https://img.alicdn.com/img/i2/2211596014329/O1CN01zmVkmr1hqjUo5CGey_!!2211596014329-0-alimamacc.jpg_360x360xzq75.jpg_.webp',
        name: '红点',
        price: 399),
    NewProductModel(
        imgUrl:
            'https://img.alicdn.com/img/i1/109702118/O1CN019jvBYL1RW5mm9g6Sf_!!0-saturn_solar.jpg_360x360xzq75.jpg_.webp',
        name: '小黑',
        price: 1999),
    NewProductModel(
        imgUrl:
            'https://gw.alicdn.com/imgextra/i4/1879944864/O1CN01ZKXBpx1lnlLcLnxN7_!!1879944864.jpg_Q75.jpg_.webp',
        name: 'Mate40Pro',
        price: 3999),
    NewProductModel(
        imgUrl:
            'https://gw.alicdn.com/imgextra/i3/1959202531/O1CN01brIKy11UZFL2cy4Qv_!!1959202531.jpg_Q75.jpg_.webp',
        name: 'DOOV朵唯H8超薄',
        price: 499),
    NewProductModel(
        imgUrl:
            'https://gw.alicdn.com/imgextra/i3/2783679443/O1CN01k3LNwj2Jcwu2Yx55c_!!2783679443.jpg_Q75.jpg_.webp',
        name: 'VIVO iQOO',
        price: 2999),
    NewProductModel(
        imgUrl:
            'https://gw.alicdn.com/imgextra/i4/2211621917396/O1CN01Bpsdjt24VQ3YPdI6N_!!2211621917396.jpg_Q75.jpg_.webp',
        name: '飞利-老人机',
        price: 199)
  ];

  List<PopularityModel> popularityList = [
    PopularityModel(
        name: '新品上市realme真我12 Pro+ 官方正品 512g大内存 大电池容量手机 新',
        imgUrl:
            'https://gw.alicdn.com/imgextra/i3/2200766936524/O1CN01KPdJ851y42rO4w1zW_!!2200766936524.jpg_Q75.jpg_.webp',
        price: 1999),
    PopularityModel(
        name: '【新品上市】realme真我11 天玑6020旗舰芯 旗舰影像大内存新品学生拍照手机5g realme 11',
        imgUrl:
            'https://gw.alicdn.com/imgextra/i4/2200766936524/O1CN01VamJmZ1y42rJYEdcI_!!2200766936524.png_.webp',
        price: 999),
    PopularityModel(
        name: 'HONOR/荣耀X50 ',
        imgUrl:
            'https://img.alicdn.com/img/i1/469430164/O1CN01yekrZ11D59sYQLQbt_!!0-saturn_solar.jpg_360x360xzq75.jpg_.webp',
        price: 899),
    PopularityModel(
        name: '【分期免息】Apple/苹果 iPhone 13手机苹果13全网通5G手机',
        imgUrl:
            'https://img.alicdn.com/bao/uploaded/i3/2208129596443/O1CN01wxYLmQ1xSwl3e3zG1_!!2208129596443.jpg',
        price: 1399),
    PopularityModel(
        name: '【新品上市】realme真我12 Pro+ 官方正品 512g大内存 大电池容量手机 新款拍照手机智能机学生老人机备用机',
        imgUrl:
            'https://img.alicdn.com/bao/uploaded/i3/2200766936524/O1CN014gaYxa1y42rqHyxoc_!!0-item_pic.jpg',
        price: 2999),
    PopularityModel(
        name: '【新品上市】真我V50s移动版天玑6100+ 5000mAh大电池超大内存5G手机学生游戏官方正品v50s',
        imgUrl:
            'https://img.alicdn.com/bao/uploaded/i1/2200766936524/O1CN01STXGV81y42q7Y5izc_!!0-item_pic.jpg',
        price: 799),
    PopularityModel(
        name: '【512版本立省100元】realme真我GT5 Pro旗舰新机第三代骁龙8潜望长焦ai手机官方正品游戏手机gt5Pro',
        imgUrl:
            'https://img.alicdn.com/bao/uploaded/i1/2200766936524/O1CN01XUAAJl1y42roadvqU_!!0-item_pic.jpg',
        price: 2899),
    PopularityModel(
        name: '【当天发货+送碎屏保】HONOR/荣耀80SE 5G手机官方旗舰店官网正品荣耀80Pro系列新品华90为曲面屏新款手机',
        imgUrl:
            'https://img.alicdn.com/bao/uploaded/i3/2204987709980/O1CN01wMpup12NatdqdkccN_!!0-item_pic.jpg',
        price: 899)
  ];

  Timer? _timer;

  RxInt timeout = (11 * 3600 - 1).obs;

  @override
  void onInit() {
    _timer = Timer.periodic(const Duration(seconds: 1), (Timer timer) {
      if (timeout.value == 0) {
        return _timer?.cancel();
      }
      timeout--;
    });
    super.onInit();
  }

  @override
  void onClose() {
    clearTimeout();
    super.onClose();
  }

  void clearTimeout() {
    if (_timer != null && _timer!.isActive) {
      _timer?.cancel();
    }
  }
}

class HomeIndexView extends StatefulWidget {
  const HomeIndexView({super.key});

  @override
  State<HomeIndexView> createState() => _HomeIndexViewState();
}

class _HomeIndexViewState extends State<HomeIndexView> {
  late HomeIndexController controller;

  @override
  void initState() {
    controller = Get.find<HomeIndexController>();
    super.initState();
  }

  Widget buildMain(BuildContext context) {
    return Scaffold(
      backgroundColor: ColorsUtils.parseColorFromHexString(ColorsUtils.colorF2f2),
      appBar: PreferredSize(
        preferredSize: const Size.fromHeight(kToolbarHeight),
        child: AnnotatedRegion<SystemUiOverlayStyle>(
          value: SystemUiOverlayStyle.light,
          child: Container(
            padding: EdgeInsets.only(top: Get.mediaQuery.padding.top),
            decoration: BoxDecoration(
              color: ColorsUtils.parseColorFromHexString(ColorsUtils.colorF048),
            ),
            alignment: Alignment.center,
            child: SizedBox(
              height: 35.h,
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Expanded(
                      child: Center(
                    child: SvgUtils.assetName('u375', width: 21.w, height: 20.h),
                  )),
                  Container(
                    width: 289.w,
                    height: 35.h,
                    alignment: Alignment.centerLeft,
                    decoration: BoxDecoration(
                        color: ColorsUtils.parseColorFromHexString(ColorsUtils.colorFfff),
                        borderRadius: BorderRadius.circular(17.5.r)),
                    padding: EdgeInsets.symmetric(horizontal: 15.w),
                    child: TextField(
                      readOnly: true,
                      decoration: InputDecoration(
                          hintText: '',
                          hintStyle: StylesUtils.customTextStyle(
                              color: ColorsUtils.color9999, fontSize: StylesUtils.fontSize13),
                          border: InputBorder.none,
                          focusedBorder: InputBorder.none,
                          isCollapsed: false,
                          isDense: false,
                          prefixIconConstraints: const BoxConstraints.expand(width: 15, height: 15),
                          prefixIcon: SvgUtils.assetName('shape_u373', width: 14.w, height: 14.h),
                          contentPadding: const EdgeInsets.symmetric(vertical: 10),
                          constraints: const BoxConstraints.expand(height: 30)),
                      onTap: () {
                        Get.toNamed(RoutePath.searchProduct);
                      },
                    ),
                  ),
                  Expanded(
                      child: InkWell(
                    onTap: () {
                      eventBus.fire(HomeChangeIndexEvent(index: 3));
                    },
                    child: Center(
                      child: Stack(
                        clipBehavior: Clip.none,
                        children: [
                          SizedBox(
                            width: 20,
                            height: 20,
                            child: SvgUtils.assetName('u377', width: 20, height: 20),
                          ),
                          Positioned(
                              top: -5,
                              right: -5,
                              child: Container(
                                alignment: Alignment.center,
                                constraints: const BoxConstraints.expand(width: 18, height: 18),
                                decoration: BoxDecoration(
                                    borderRadius: BorderRadius.circular(9),
                                    color:
                                        ColorsUtils.parseColorFromHexString(ColorsUtils.colorF95e)),
                                child: Text(
                                  '6',
                                  style: StylesUtils.customTextStyle(
                                    fontSize: StylesUtils.fontSize8,
                                    color: ColorsUtils.colorFfff,
                                  ),
                                ),
                              ))
                        ],
                      ),
                    ),
                  ))
                ],
              ),
            ),
          ),
        ),
      ),
      body: SingleChildScrollView(
        child: Column(
          children: [
            SizedBox(
              height: 146.h,
              child: Swiper.children(
                  autoplay: true,
                  duration: 500,
                  autoplayDelay: 5000,
                  pagination: SwiperPagination(
                      builder: DotSwiperPaginationBuilder(
                          activeColor: Colors.white,
                          color: ColorsUtils.parseColorFromHexString(ColorsUtils.color6666),
                          size: 10.w,
                          activeSize: 10.w),
                      alignment: Alignment.bottomCenter),
                  children: controller.topList
                      .map((e) => CachedNetworkImage(
                            imageUrl: e,
                            width: double.infinity,
                            height: 146.h,
                            fit: BoxFit.cover,
                          ))
                      .toList()),
            ),
            Container(
              padding: EdgeInsets.only(top: 15.h, left: 20.w, right: 20.w, bottom: 15.h),
              decoration: const BoxDecoration(color: Colors.white),
              child: StaggeredGrid.count(
                crossAxisCount: 4,
                mainAxisSpacing: 20.h,
                crossAxisSpacing: 20.w,
                children: controller.navTabs.map((e) {
                  return InkWell(
                    onTap: () {
                      Get.toNamed(e.url);
                    },
                    child: Column(
                      children: [
                        Center(
                          child: Container(
                            constraints: BoxConstraints.expand(width: 50.w, height: 40.h),
                            decoration: BoxDecoration(
                                borderRadius: BorderRadius.circular(5.r),
                                color: ColorsUtils.parseColorFromHexString(ColorsUtils.colorFbb8)),
                            alignment: Alignment.center,
                            child: SvgUtils.assetName('u144', width: 20.w, height: 15.h),
                          ),
                        ),
                        Container(
                          margin: EdgeInsets.only(top: 5.h),
                          alignment: Alignment.center,
                          child: Text(
                            e.name,
                            style: StylesUtils.customTextStyle(
                                fontSize: StylesUtils.fontSize13, color: ColorsUtils.color3333),
                          ),
                        )
                      ],
                    ),
                  );
                }).toList(),
              ),
            ),
            Container(
              margin: EdgeInsets.only(top: 15.h),
              padding: EdgeInsets.only(bottom: 18.h),
              decoration: const BoxDecoration(color: Colors.white),
              child: Column(
                children: [
                  Container(
                    height: 40.h,
                    padding: EdgeInsets.only(left: 20.w, right: 10.w),
                    alignment: Alignment.centerLeft,
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.start,
                      children: [
                        Container(
                          alignment: Alignment.centerLeft,
                          child: Text(
                            '秒杀专场',
                            style: StylesUtils.customTextStyle(
                                color: ColorsUtils.color0000,
                                fontSize: StylesUtils.fontSize16,
                                fontWeight: FontWeight.w700),
                          ),
                        ),
                        Container(
                          margin: EdgeInsets.only(left: 15.w, right: 10.w),
                          alignment: Alignment.center,
                          child: Text(
                            '10点场',
                            style: StylesUtils.customTextStyle(
                                fontWeight: FontWeight.w500, color: ColorsUtils.color3333),
                          ),
                        ),
                        Container(
                          constraints: BoxConstraints.expand(width: 24.w, height: 20.h),
                          alignment: Alignment.center,
                          decoration: BoxDecoration(color: ColorsUtils.rgba(242, 111, 81, 1)),
                          child: Obx(() => Text(
                                durationTransform(controller.timeout.value, 1),
                                style: StylesUtils.customTextStyle(
                                  fontSize: StylesUtils.fontSize12,
                                ),
                              )),
                        ),
                        Container(
                          margin: EdgeInsets.symmetric(horizontal: 5.w),
                          alignment: Alignment.center,
                          child: Text(
                            '：',
                            style: StylesUtils.customTextStyle(color: ColorsUtils.color3333),
                          ),
                        ),
                        Container(
                          constraints: BoxConstraints.expand(width: 24.w, height: 20.h),
                          alignment: Alignment.center,
                          decoration: BoxDecoration(color: ColorsUtils.rgba(242, 111, 81, 1)),
                          child: Obx(() => Text(
                                durationTransform(controller.timeout.value, 2),
                                style: StylesUtils.customTextStyle(
                                  fontSize: StylesUtils.fontSize12,
                                ),
                              )),
                        ),
                        Container(
                          margin: EdgeInsets.symmetric(horizontal: 5.w),
                          alignment: Alignment.center,
                          child: Text(
                            '：',
                            style: StylesUtils.customTextStyle(color: ColorsUtils.color3333),
                          ),
                        ),
                        Container(
                          constraints: BoxConstraints.expand(width: 24.w, height: 20.h),
                          alignment: Alignment.center,
                          decoration: BoxDecoration(color: ColorsUtils.rgba(242, 111, 81, 1)),
                          child: Obx(() => Text(
                                durationTransform(controller.timeout.value, 3),
                                style: StylesUtils.customTextStyle(
                                  fontSize: StylesUtils.fontSize12,
                                ),
                              )),
                        ),
                        Expanded(
                            child: Container(
                          alignment: Alignment.centerRight,
                          child: TextButton(
                              onPressed: () {
                                Get.toNamed(RoutePath.seckillList);
                              },
                              child: Text(
                                '更多',
                                style: StylesUtils.customTextStyle(color: ColorsUtils.color9999),
                              )),
                        ))
                      ],
                    ),
                  ),
                  Container(
                    padding: EdgeInsets.symmetric(horizontal: 15.w),
                    child: StaggeredGrid.count(
                      crossAxisCount: 4,
                      mainAxisSpacing: 15.h,
                      crossAxisSpacing: 10.w,
                      children: controller.sales.map((e) {
                        return Column(
                          children: [
                            Center(
                              child: ClipRRect(
                                borderRadius: BorderRadius.circular(5.r),
                                child: CachedNetworkImage(
                                  imageUrl: e,
                                  width: 106.w,
                                  height: 84.h,
                                  fit: BoxFit.cover,
                                ),
                              ),
                            ),
                            Container(
                              alignment: Alignment.centerLeft,
                              margin: EdgeInsets.only(top: 8.h),
                              child: Text(
                                '199',
                                style: StylesUtils.customTextStyle(color: ColorsUtils.colorF95e),
                              ),
                            ),
                            Container(
                              alignment: Alignment.centerLeft,
                              margin: EdgeInsets.only(top: 5.h),
                              child: Text(
                                '￥399',
                                style: StylesUtils.customTextStyle(
                                    fontSize: StylesUtils.fontSize12,
                                    color: ColorsUtils.color9999,
                                    decorationColor:
                                        ColorsUtils.parseColorFromHexString(ColorsUtils.color9999),
                                    decorationStyle: TextDecorationStyle.solid,
                                    decorationThickness: 1.0,
                                    decoration: TextDecoration.lineThrough),
                              ),
                            )
                          ],
                        );
                      }).toList(),
                    ),
                  ),
                ],
              ),
            ),
            Container(
              margin: EdgeInsets.only(top: 15.h),
              padding: EdgeInsets.only(bottom: 18.h),
              decoration: const BoxDecoration(color: Colors.white),
              child: Column(
                children: [
                  Container(
                    height: 40.h,
                    padding: EdgeInsets.only(left: 25.w, right: 10.w),
                    alignment: Alignment.centerLeft,
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Text(
                          '品牌制造直供',
                          style: StylesUtils.customTextStyle(
                              color: ColorsUtils.color0000,
                              fontSize: StylesUtils.fontSize16,
                              fontWeight: FontWeight.w700),
                        ),
                        TextButton(
                            onPressed: () {},
                            child: Text(
                              '更多',
                              style: StylesUtils.customTextStyle(color: ColorsUtils.color9999),
                            ))
                      ],
                    ),
                  ),
                  Container(
                    padding: EdgeInsets.only(left: 15.w, right: 10.w),
                    child: StaggeredGrid.count(
                      crossAxisCount: 2,
                      crossAxisSpacing: 10.w,
                      mainAxisSpacing: 15.h,
                      children: controller.brandList
                          .map((e) => Column(
                                children: [
                                  Center(
                                    child: ClipRRect(
                                      borderRadius: BorderRadius.circular(5.r),
                                      child: CachedNetworkImage(
                                        imageUrl: e.imgUrl,
                                        width: 160.w,
                                        height: 84.h,
                                        fit: BoxFit.fill,
                                      ),
                                    ),
                                  ),
                                  Container(
                                      margin: EdgeInsets.only(top: 8.h),
                                      alignment: Alignment.centerLeft,
                                      child: Row(
                                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                        children: [
                                          Text(
                                            e.name,
                                            style: StylesUtils.customTextStyle(
                                                color: ColorsUtils.color3333),
                                          ),
                                          RichText(
                                              text: TextSpan(children: [
                                            TextSpan(
                                                text: '￥${e.price}',
                                                style: StylesUtils.customTextStyle(
                                                    color: ColorsUtils.colorF95e,
                                                    fontSize: StylesUtils.fontSize12))
                                          ]))
                                        ],
                                      ))
                                ],
                              ))
                          .toList(),
                    ),
                  )
                ],
              ),
            ),
            Container(
              margin: EdgeInsets.only(top: 15.h),
              padding: EdgeInsets.only(bottom: 18.h),
              decoration: const BoxDecoration(color: Colors.white),
              child: Column(
                children: [
                  Container(
                    height: 40.h,
                    padding: EdgeInsets.only(left: 25.w, right: 10.w),
                    alignment: Alignment.centerLeft,
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Text(
                          '优品推荐',
                          style: StylesUtils.customTextStyle(
                              color: ColorsUtils.color0000,
                              fontSize: StylesUtils.fontSize16,
                              fontWeight: FontWeight.w700),
                        ),
                        TextButton(
                            onPressed: () {},
                            child: Text(
                              '更多',
                              style: StylesUtils.customTextStyle(color: ColorsUtils.color9999),
                            ))
                      ],
                    ),
                  ),
                  Container(
                    padding: EdgeInsets.only(left: 15.w, right: 10.w),
                    child: StaggeredGrid.count(
                      crossAxisCount: 2,
                      crossAxisSpacing: 10.w,
                      mainAxisSpacing: 15.h,
                      children: controller.superiors
                          .map((e) => Column(
                                children: [
                                  Center(
                                    child: ClipRRect(
                                      borderRadius: BorderRadius.circular(5.r),
                                      child: CachedNetworkImage(
                                        imageUrl: e.logoUrl,
                                        width: 160.w,
                                        height: 84.h,
                                        fit: BoxFit.cover,
                                      ),
                                    ),
                                  ),
                                  Container(
                                    margin: EdgeInsets.only(top: 10.h),
                                    alignment: Alignment.centerLeft,
                                    child: Text(
                                      e.name,
                                      style:
                                          StylesUtils.customTextStyle(color: ColorsUtils.color3333),
                                    ),
                                  )
                                ],
                              ))
                          .toList(),
                    ),
                  )
                ],
              ),
            ),
            Container(
              margin: EdgeInsets.only(top: 15.h),
              padding: EdgeInsets.only(bottom: 18.h),
              decoration: const BoxDecoration(color: Colors.white),
              child: Column(
                children: [
                  Container(
                    height: 40.h,
                    padding: EdgeInsets.only(left: 25.w, right: 10.w),
                    alignment: Alignment.centerLeft,
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Text(
                          '新品首发',
                          style: StylesUtils.customTextStyle(
                              color: ColorsUtils.color0000,
                              fontSize: StylesUtils.fontSize16,
                              fontWeight: FontWeight.w700),
                        ),
                        TextButton(
                            onPressed: () {
                              Get.toNamed(RoutePath.shopNews);
                            },
                            child: Text(
                              '更多',
                              style: StylesUtils.customTextStyle(color: ColorsUtils.color9999),
                            ))
                      ],
                    ),
                  ),
                  Container(
                    padding: EdgeInsets.only(left: 15.w, right: 10.w),
                    child: StaggeredGrid.count(
                      crossAxisCount: 2,
                      crossAxisSpacing: 10.w,
                      mainAxisSpacing: 15.h,
                      children: controller.productList
                          .map((e) => Column(
                                children: [
                                  Center(
                                    child: ClipRRect(
                                      borderRadius: BorderRadius.circular(5.r),
                                      child: CachedNetworkImage(
                                        imageUrl: e.imgUrl,
                                        width: 160.w,
                                        height: 84.h,
                                        fit: BoxFit.contain,
                                      ),
                                    ),
                                  ),
                                  Container(
                                    margin: EdgeInsets.only(top: 8.h),
                                    alignment: Alignment.centerLeft,
                                    child: Text(
                                      e.name,
                                      style:
                                          StylesUtils.customTextStyle(color: ColorsUtils.color3333),
                                    ),
                                  ),
                                  Container(
                                    margin: EdgeInsets.only(top: 4.h),
                                    alignment: Alignment.centerLeft,
                                    child: Text(
                                      '￥${e.price}',
                                      style:
                                          StylesUtils.customTextStyle(color: ColorsUtils.colorF95e),
                                    ),
                                  )
                                ],
                              ))
                          .toList(),
                    ),
                  )
                ],
              ),
            ),
            Container(
              margin: EdgeInsets.only(top: 15.h, bottom: 20.h),
              padding: EdgeInsets.only(bottom: 18.h),
              decoration: const BoxDecoration(color: Colors.white),
              child: ListBody(
                children: [
                  Container(
                    height: 40.h,
                    padding: EdgeInsets.only(left: 25.w, right: 10.w),
                    alignment: Alignment.centerLeft,
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Text(
                          '人气推荐',
                          style: StylesUtils.customTextStyle(
                              color: ColorsUtils.color0000,
                              fontSize: StylesUtils.fontSize16,
                              fontWeight: FontWeight.w700),
                        ),
                        TextButton(
                            onPressed: () {
                              Get.toNamed(RoutePath.recommendedPopularity);
                            },
                            child: Text(
                              '更多',
                              style: StylesUtils.customTextStyle(color: ColorsUtils.color9999),
                            ))
                      ],
                    ),
                  ),
                  ...controller.popularityList.asMap().map((i, e) {
                    return MapEntry(
                        i,
                        Container(
                          padding: EdgeInsets.only(
                            left: 15.w,
                            right: 10.w,
                          ),
                          child: Container(
                            margin: EdgeInsets.only(
                                bottom: i == controller.popularityList.length - 1 ? 0 : 15.h),
                            child: Row(
                              mainAxisAlignment: MainAxisAlignment.spaceBetween,
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                ClipRRect(
                                  borderRadius: BorderRadius.circular(5.r),
                                  child: CachedNetworkImage(
                                    imageUrl: e.imgUrl,
                                    width: 160.w,
                                    height: 103.h,
                                    fit: BoxFit.contain,
                                  ),
                                ),
                                Expanded(
                                    child: Container(
                                  padding: EdgeInsets.only(left: 10.w, top: 10.h),
                                  child: Column(
                                    children: [
                                      Container(
                                        alignment: Alignment.centerLeft,
                                        child: Text(
                                          e.name,
                                          maxLines: 2,
                                          overflow: TextOverflow.ellipsis,
                                          style: StylesUtils.customTextStyle(
                                              color: ColorsUtils.color3333, lineHeight: 1.5.h),
                                        ),
                                      ),
                                      Container(
                                        margin: EdgeInsets.only(top: 10.h),
                                        alignment: Alignment.centerLeft,
                                        child: Text(
                                          '￥${e.price}',
                                          style: StylesUtils.customTextStyle(
                                              color: ColorsUtils.colorF95e),
                                        ),
                                      )
                                    ],
                                  ),
                                ))
                              ],
                            ),
                          ),
                        ));
                  }).values
                ],
              ),
            )
          ],
        ),
      ),
    );
  }

  @override
  void dispose() {
    Get.delete<HomeIndexController>();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return buildMain(context);
  }

  String durationTransform(int seconds, int index) {
    if (seconds > 0) {
      String str = Duration(seconds: seconds).toString();
      RegExp regExp = RegExp(r'(\d+):(\d+):(\d+)(\.\d+)?$');
      Match? match = regExp.matchAsPrefix(str);
      if (match != null) {
        return match.group(index) ?? '00';
      }
    }
    return '00';
  }
}

class _NavTabModel {
  String url;

  String name;

  _NavTabModel({required this.name, required this.url});
}
